var express = require("express");
var router = express.Router();
var connection = require("../db/sql");

/* GET home page. */
router.get("/", function (req, res, next) {
  // res.render("index", { title: "Express" });
  res.send("Hello World");
});

/**
 * 首页数据
 */
router.get("/index", function (req, res, next) {
  res.send({
    code: 0,
    msg: "ok",
    data: {
      sortList: [
        {
          id: 1,
          title: "女装",
          imgUrl: "./public/images/1.webp",
        },
        {
          id: 2,
          title: "女鞋",
          imgUrl: "./public/images/2.webp",
        },
        {
          id: 3,
          title: "上衣",
          imgUrl: "./public/images/3.webp",
        },
        {
          id: 4,
          title: "美妆/护肤",
          imgUrl: "./public/images/4.webp",
        },
        {
          id: 5,
          title: "裤子",
          imgUrl: "./public/images/5.webp",
        },
        {
          id: 6,
          title: "母婴/童装",
          imgUrl: "./public/images/6.webp",
        },
        {
          id: 7,
          title: "裙子",
          imgUrl: "./public/images/7.webp",
        },
        {
          id: 8,
          title: "男装男鞋",
          imgUrl: "./public/images/8.webp",
        },
        {
          id: 9,
          title: "套装",
          imgUrl: "./public/images/9.webp",
        },
        {
          id: 10,
          title: "配饰",
          imgUrl: "./public/images/10.webp",
        },
      ],
      goodsList: [
        {
          id: 1,
          name: "新中式扎染国风A字夏季半身裙MS-2157",
          avator: "./public/images/t-1.webp",
          avatorname: "冉冉",
          avatortag: "好货聚集地",
          price: "79.9",
          sales: "133",
          imgUrl: "./public/images/s-1.webp",
          leftImgUrl: "./public/images/zhibotongjia.png",
          rightImgUrl: "./public/images/zhibo.png",
          byImgUrl: "./public/images/baoyou.png",
        },
        {
          id: 2,
          name: "4618别致漂亮雪纺小衫女2023年宽松泡泡袖洋气减龄上衣",
          avator: "./public/images/t-2.webp",
          avatorname: "小甜心_呢",
          avatortag: "全品类女王",
          price: "39.9",
          sales: "849",
          imgUrl: "./public/images/s-2.webp",
          leftImgUrl: "./public/images/zhibotongjia.png",
          rightImgUrl: "./public/images/zhibo.png",
          byImgUrl: "./public/images/baoyou.png",
        },
        {
          id: 3,
          name: "4618别致漂亮雪纺小衫女2023年宽松泡泡袖洋气减龄上衣",
          avator: "./public/images/t-3.webp",
          avatorname: "小甜心_呢",
          avatortag: "好货聚集地",
          price: "39.9",
          sales: "849",
          imgUrl: "./public/images/s-3.webp",
          leftImgUrl: "./public/images/zhibotongjia.png",
          rightImgUrl: "./public/images/zhibo.png",
          byImgUrl: "./public/images/baoyou.png",
        },
        {
          id: 4,
          name: "4618别致漂亮雪纺小衫女2023年宽松泡泡袖洋气减龄上衣",
          avator: "./public/images/t-4.webp",
          avatorname: "小甜心_呢",
          avatortag: "好货聚集地",
          price: "39.9",
          sales: "849",
          imgUrl: "./public/images/s-4.webp",
          leftImgUrl: "./public/images/zhibotongjia.png",
          rightImgUrl: "./public/images/zhibo.png",
          byImgUrl: "./public/images/baoyou.png",
        },
        {
          id: 5,
          name: "4618别致漂亮雪纺小衫女2023年宽松泡泡袖洋气减龄上衣",
          avator: "./public/images/t-5.webp",
          avatorname: "小甜心_呢",
          avatortag: "好货聚集地",
          price: "39.9",
          sales: "849",
          imgUrl: "./public/images/s-5.webp",
          leftImgUrl: "./public/images/zhibotongjia.png",
          rightImgUrl: "./public/images/zhibo.png",
          byImgUrl: "./public/images/baoyou.png",
        },
        {
          id: 6,
          name: "4618别致漂亮雪纺小衫女2023年宽松泡泡袖洋气减龄上衣",
          avator: "./public/images/t-6.webp",
          avatorname: "小甜心_呢",
          avatortag: "好货聚集地",
          price: "39.9",
          sales: "849",
          imgUrl: "./public/images/s-6.webp",
          leftImgUrl: "./public/images/zhibotongjia.png",
          rightImgUrl: "./public/images/zhibo.png",
          byImgUrl: "./public/images/baoyou.png",
        },
      ],
    },
  });
});

router.get("/searchList", function (req, res, next) {
  res.send({
    code: 0,
    msg: "ok",
    data: {
      searchList: [
        {
          id: 1,
          name: "港味牛仔外套2019春秋韩版无袖牛仔衣学生bf宽松工装马甲女",
          price: "39.9",
          collction: "387",
          imgUrl: "./public/images/list-1.webp",
        },
        {
          id: 3,
          name: "港味牛仔外套2019春秋韩版无袖牛仔衣学生bf宽松工装马甲女",
          price: "39.9",
          collction: "387",
          imgUrl: "./public/images/list-2.webp",
        },
        {
          id: 8,
          name: "港味牛仔外套2019春秋韩版无袖牛仔衣学生bf宽松工装马甲女",
          price: "39.9",
          collction: "387",
          imgUrl: "./public/images/list-3.webp",
        },
        {
          id: 12,
          name: "港味牛仔外套2019春秋韩版无袖牛仔衣学生bf宽松工装马甲女",
          price: "39.9",
          collction: "387",
          imgUrl: "./public/images/list-1.webp",
        },
      ],
    },
  });
});

router.get("/goods/shopList", function (req, res, next) {
  console.log(req.query);
  const searchName = req.query.searchName;
  connection.query(
    `select * from goodslist where name like '%${searchName}%'`,
    function (err, result) {
      if (err) {
        console.error(err);
        return res.status(500).send({
          code: 500,
          msg: "服务器异常",
        });
      }
      res.send({
        code: 0,
        msg: "ok",
        data: result,
      });
    }
  );
});

router.get("/goods/list", function (req, res, next) {
  res.send({
    code: 0,
    msg: "ok",
    data: [
      {
        // 一级分类
        id: 0,
        name: "上衣",
        data: [
          {
            // 二级分类
            id: 0,
            name: "9.9秒杀",
            imgUrl: "./public/images/miaosha.webp",
          },
          {
            id: 1,
            name: "热销爆款",
            imgUrl: "./public/images/list-sy-1.webp",
          },
          {
            id: 2,
            name: "T恤",
            imgUrl: "./public/images/list-sy-2.webp",
          },
          {
            id: 3,
            name: "针织衫",
            imgUrl: "./public/images/list-sy-3.webp",
          },
          {
            id: 4,
            name: "衬衫",
            imgUrl: "./public/images/list-sy-4.webp",
          },
          {
            id: 5,
            name: "雪纺衫",
            imgUrl: "./public/images/list-sy-5.webp",
          },
          {
            id: 6,
            name: "打底背心",
            imgUrl: "./public/images/list-sy-6.webp",
          },
        ],
      },
      {
        id: 1,
        name: "裤子",
        data: [
          {
            id: 0,
            name: "9.9秒杀",
            imgUrl: "./public/images/miaosha.webp",
          },
          {
            id: 1,
            name: "牛仔裤",
            imgUrl: "./public/images/list-kz-1.webp",
          },
          {
            id: 2,
            name: "休闲裤",
            imgUrl: "./public/images/list-kz-2.webp",
          },
          {
            id: 3,
            name: "阔腿裤",
            imgUrl: "./public/images/list-kz-3.webp",
          },
          {
            id: 4,
            name: "工装裤",
            imgUrl: "./public/images/list-kz-4.webp",
          },
          {
            id: 5,
            name: "西装裤",
            imgUrl: "./public/images/list-kz-5.webp",
          },
          {
            id: 6,
            name: "打底裤",
            imgUrl: "./public/images/list-kz-6.webp",
          }
        ],
      },
    ],
  });
});

//登录
router.post("/login", function (req, res, next) {
  console.log(req.body);
  const username = req.body.tel;
  const password = req.body.pwd;
  connection.query(
    `select * from user where tel = '${username}' and pwd = '${password}'`,
    function (err, result) {
      if (err) {
        console.error(err);
        return res.status(500).send({
          code: 500,
          msg: "服务器异常",
        });
      }
      res.send({
        code: 0,
        msg: "ok",
        data: result,
      });
    }
  );
});

module.exports = router;
